const bcrypt = require('bcryptjs')
const { query } = require('./config/database')

async function main () {
  try {
    console.log('🔍 检查用户表结构...')

    // 查看表结构
    const structure = await query('DESCRIBE users')
    console.log('表结构:')
    console.table(structure)

    // 查看现有用户
    const users = await query('SELECT * FROM users')
    console.log('\n现有用户:')
    console.table(users)

    // 检查admin@novel.com是否存在
    const adminUser = await query(
      'SELECT * FROM users WHERE email = "admin@novel.com"'
    )

    if (adminUser.length === 0) {
      console.log('\n正在创建管理员账号...')
      const password = await bcrypt.hash('admin123', 12)

      await query(
        'INSERT INTO users (email, username, password, is_vip, coins) VALUES (?, ?, ?, ?, ?)',
        ['admin@novel.com', '管理员', password, 1, 9999]
      )

      console.log('✅ 管理员账号创建成功！')
      console.log('邮箱: admin@novel.com')
      console.log('密码: admin123')
    } else {
      console.log('\n✅ 管理员账号已存在')
    }
  } catch (error) {
    console.error('错误:', error.message)
  }

  process.exit(0)
}

main()
